{% extends "base.html" %}

{% block title %}{% endblock %}
{% block content %}


<div class="demoTable">
    <br>
    <form class="layui-form" action="" id="workTable">

        <div class="layui-inline">
            <label class="layui-form-label">task_name：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="task_name" id="task_name" placeholder="请输入" style="width:212px">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <br>
            <label class="layui-form-label">note：</label>
            <div class="layui-input-inline">
                    <textarea name="case_json" id="case_json" placeholder="请输入" class="layui-textarea" style="width:455%;height:300px"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button  class="layui-btn layui-btn-normal" id="save" lay-submit lay-filter="formDemo">保存</button>
                <button  class="layui-btn layui-btn-normal layui-btn-danger" id="exmple">示例</button>
            </div>
        </div>
    </form>

</div>
<script>
function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
                return true;
            }else{
                return false;
            }

        } catch(e) {

            return false;
        }
    }

}
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;

          //监听提交
          form.on('submit(formDemo)', function(data){
            if(data.field.task_name == ""){
                    layer.msg("请输入一个别名");
                    return false;
            }

            if(isJSON(data.field.case_json)){

            }else{
                layer.msg("不为json格式");
                return false;
            }

            case_json = JSON.parse(data.field.case_json)
            task_name = data.field.task_name;
            $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/save/locust/case",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify({"task_name":task_name,"case_json":case_json}),
                        success: function(result){
                            layer.msg("添加成功!");
                             $('#save').hide();
                             $('#exmple').hide();

                        }
            });

            return false;
          });


          $('#exmple').on('click',function(){

                msg = {"Request_datas":[{"Method":"","Url":"","PostBody":"","ContentType":"","DisableKeepalive":false,"Expect_code":200,"Disable_response":false,"Expect_string":"","Header_datas":[{"HeaderName":"","HeaderValue":""}]}]};
                msg = JSON.stringify(msg, null, 4)
                $("#case_json").val(msg);
                return false;
          });
          form.render();

    });

</script>

{% endblock %}